import axios from "axios";

import {
  Notification
} from "element-ui";
import router from "../router";

const durationTime = 2000;

// axios.defaults.baseURL = "/api";

const request = axios.create({
  timeout: 30000,
  baseURL: "http://localhost:9011",
  // baseURL: "http://8.130.22.127:9998",
});
//请求拦截器
request.interceptors.request.use((config) => {
  config.headers["token-name"] = window.sessionStorage.getItem("OaAuth");
  config.headers["AuthorBBS"] = window.sessionStorage.getItem("bbsToken");
  return config;
});

//响应拦截器
request.interceptors.response.use(
  function (response) {
    if (
      response.status &&
      response.status === 200 &&
      response.data.code == 99
    ) {}
    if (
      response.status &&
      response.status === 200 &&
      response.data.code === 500
    ) {
      Notification.error({
        message: response.data.message,
        duration: durationTime,
      });
      return;
    }
    if (
      response.status &&
      response.status === 200 &&
      response.data.code == 200
    ) {
      if (response.data.data != null) {
        if (document.getElementsByClassName("el-notification").length > 0) {} else {
          if (response.data.message != "" || response.data.message != null) {
            Notification.success({
              message: response.data.message,
              duration: durationTime,
            });
          }
        }
      } else {
        if (response.data.message != "" || response.data.message != null) {
          Notification.success({
            message: response.data.message,
            duration: durationTime,
          });
        }
      }
    }
    if (
      response.status &&
      response.status === 401 &&
      response.data.code === 401
    ) {
      Notification.error({
        message: response.data.message,
        duration: durationTime,
      });
      return;
    }
    return response.data;
  },
  function (error) {
    let respStatus = error.response.status;
    let respResult = error.response;

    if (respStatus == 401 && respResult.data.code == 401) {
      Notification.error({
        message: respResult.data.message != null ?
          respResult.data.message : "尚未登录，请登录",
        duration: durationTime,
      });
      router.replace("/login");
    } else if (respStatus == 401 && respResult.data.code == 500) {
      Notification.error({
        message: respResult.data.message != null ?
          respResult.data.message : "应用出现错误，请联系管理员",
        duration: durationTime,
      });
    } else if (respStatus == 403 && respResult.data.code == 403) {
      Notification.error({
        message: respResult.data.message != null ?
          respResult.data.message : "无访问权限",
        duration: durationTime,
      });
    } else if (
      respStatus == 404 ||
      (respStatus == 504 && respResult.data.code == 403)
    ) {
      Notification.error({
        message: respResult.data.message != null ?
          respResult.data.message : "无访问权限",
        duration: durationTime,
      });
    } else if (respStatus == 500) {
      Notification.error({
        message: respResult.data.message != null ?
          respResult.data.message : "无访问权限",
        duration: durationTime,
      });
    } else {
      Notification.error({
        message: "未知错误",
        duration: durationTime,
      });
    }

    return;

    // return Promise.reject(error);
  }
);

export default request;

// let base="http://localhost:8888"

// export const doPost = (url, params) => {
//     return axios({
//         method: 'post',
//         url: `${base}${url}`,
//         data: params
//     })
// }

// export const doGet = (url, params) => {
//     return axios({
//         method: 'get',
//         url: `${base}${url}`,
//         data: params
//     })
// }